using System;
using System.Collections.Generic;
using FwkLight.Domain.Query;
using FwkLight.Persistence;

namespace FwkLight.Application
{
  public abstract class BaseQueryTask<Type> : BaseQueryTask<Type, Type>
  {
    protected BaseQueryTask ( INHUnitOfWorkProvider uowProvider )
      : base(uowProvider)
    {
    }
  }

  public abstract class BaseListQueryTask<Type> : BaseQueryTask<Type, IList<Type>> where Type : class
  {
    protected BaseListQueryTask ( INHUnitOfWorkProvider uowProvider )
      : base(uowProvider)
    {
    }
  }

  public abstract class BaseQueryTask<TEntityType, TReturnType> : BaseApplicationTask<TReturnType>
  {
    protected BaseQueryTask ( INHUnitOfWorkProvider uowProvider )
      : base(uowProvider)
    {
    }

    protected override void ExecuteInternal ( )
    {
      if (KeepsEntityInCache && Entity != null)
        return;

      if (KeepsEntityInCacheShort && Entity != null)
        return;

      if (KeepsEntityForCurrentRequest && Entity != null)
        return;

      base.ExecuteInternal();

      TReturnType result = ExecuteQuery();
      Entity = result;
    }

    protected virtual TReturnType ExecuteQuery()
    {
      return Query.Initialise(UOW.Session, IncludeInactive).GetResult();
    }

    protected virtual bool IncludeInactive { get { return false; } }

    protected virtual IDomainQuery<TEntityType, TReturnType> Query
    {
      get
      {
        return null;
      }
    }

  }
}
